<?php
class item_postcard extends template_item
{
	var $default_name = "Send a Postcard";
	var $default_description = "Send a postcard to someone.";
	
	var $allow_multiple = 1;
	
	var $item_config = array();
	
	function load_item(&$item)
	{
		parent::load_item($item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->item_config = unserialize($item['item_config']);
		}
		else
		{
			$this->item_config['obey_pm'] = 0;
			$this->item_config['message'] = "Postcard:\n{extra_msg}";
		}
	}
	
	function admin_form()
	{
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Does the postcard has to follow disabled PM/group PM limit/blocked member set by the recipient?</b>",
															$this->ipsclass->adskin->form_yes_no("item[".$this->item['item_id']."][obey_pm]", $this->item_config['obey_pm']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Postcard Message Tags</b>",
															"{extra_msg} = The custom message left by the sender. (Required to have)",
													)	);
		
		$message = $this->item_config['message'];
		if ( $this->ipsclass->han_editor->method == 'rte' )
		{
			$message = $this->ipsclass->parser->convert_ipb_html_to_html( $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( $message ) ) );
		}
		else
		{
			$message = $this->ipsclass->parser->pre_edit_parse( $message );
		}
		$html .= $this->ipsclass->adskin->add_td_basic("<b>Postcard Message</b><br />".$this->ipsclass->han_editor->show_editor( $message, 'item_'.$this->item['item_id'].'_message' ), 'left', 'tablerow1');
		
		return $html;
	}
	
	function admin_save(&$item_info, $input)
	{
		$save = array();
		$save['obey_pm'] = $input['obey_pm'] & 1;
		if($this->ipsclass->input['code'] != 'convert')
		{
			$input['message'] = $this->ipsclass->han_editor->process_raw_post( 'item_'.$this->item['item_id'].'_message' );
		}
		$save['message'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $input['message'] ) );
		
		$item_info['item_config'] = serialize($save);
	}
	
	function public_use_form($inventory_ids = array())
	{
		$this->ipsclass->load_language('lang_market_items');
		
 		$this->ipsclass->han_editor->remove_side_panel = 1;
 		$this->ipsclass->han_editor->remove_emoticons  = 1;
        
        $editor = $this->ipsclass->han_editor->show_editor( '', 'card_message' );
        
		$hidden_fields = $this->_get_hidden_fields($inventory_ids);
		
		return <<<EOF
<script type="text/javascript" src='jscripts/ipb_xhr_findnames.js'></script>
<div id='ipb-get-members' style='border:1px solid #000; background:#FFF; padding:2px;position:absolute;width:210px;display:none;z-index:1'></div>
<div class='formsubtitle'>{$this->ipsclass->lang['change_other_signature']}</div>
<form action='{$this->base_url}' method='post' id='changeform'>
{$hidden_fields}
	<table class='ipbtable' cellspacing='0' cellpadding='0'>
		<tr>
			<td class='row1' align='left'>
				<b>{$this->ipsclass->lang['which_member']}</b>
			</td>
			<td class='row2' align='right'>
				<input type='input' id='member_name' name='member_name' value='' size='30' class='forminput' autocomplete='off' style='width:210px' />
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='left' colspan='2'>
				<b>{$this->ipsclass->lang['postcard_msg_to_card']}</b>
			</td>
		</tr>
		<tr>
			<td class='row1' align='left' colspan='2'>
{$editor}
			</td>
		</tr>
		<tr>
			<td class='darkrow1' align='center' colspan='2'>
				<input type='submit' name='item_used' value='{$this->ipsclass->lang['postcard_send_card']}' class='forminput' />
			</td>
		</tr>
	</table>
</form>
<script type="text/javascript">
	// INIT find names
	init_js( 'changeform', 'member_name');
	// Run main loop
	setTimeout( 'main_loop()', 10 );
</script>
EOF;
	}
	
	function input_done()
	{
		return isset($this->ipsclass->input['member_name']) && isset($this->ipsclass->input['card_message']) && isset($this->ipsclass->input['item_used']);
	}
	
	function public_use_item($inventory_ids = array()) 
	{
		$this->ipsclass->load_language('lang_market_items');
		
		$member = $this->ipsclass->DB->build_and_exec_query(array('select'=>'*','from'=>'members','where'=>"members_l_display_name='".strtolower($this->ipsclass->input['member_name'])."'"));
		if(!$member['id'])
		{
			$this->ipsclass->Error(array('MSG'=>'no_user'));
		}
		
		$this->ipsclass->input['card_message'] = $this->ipsclass->parser->pre_edit_parse( $this->ipsclass->parser->pre_db_parse( $this->ipsclass->han_editor->process_raw_post('card_message') ) );
		
		if ($this->ipsclass->parser->error != "")
		{
			$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => $this->ipsclass->parser->error) );
		}
		
		require_once( ROOT_PATH.'sources/lib/func_msg.php' );
 		$msg_lib           =  new func_msg();
		$msg_lib->ipsclass =& $this->ipsclass;
		$msg_lib->init();
		
		$msg_lib->to_by_id    = $member['id'];
		$msg_lib->from_member = $this->ipsclass->member;
		$msg_lib->msg_title   = $this->ipsclass->lang['postcard_postcard'];
		$msg_lib->msg_post    = $this->ipsclass->parser->pre_display_parse( $this->ipsclass->parser->pre_db_parse( str_replace("{extra_msg}",$this->ipsclass->input['card_message'],$this->item_config['message']) ) );
		$msg_lib->force_pm    = 1;
		
		$msg_lib->send_pm();
		
		if ($msg_lib->error != "")
		{
			$this->ipsclass->lang['postcard_error'] = $msg_lib->error;
			$this->ipsclass->Error( array( 'LEVEL' => 1, 'MSG' => 'postcard_error' ) );
		}
		
		$this->delete_items($inventory_ids);
		$inv_id = reset($inventory_ids);
		$this->_write_log($inv_id, "Sent postcard to <a href='{$this->ipsclass->base_url}showuser={$member['id']}'>{$member['members_display_name']}</a>");
		
		$this->ipsclass->market_class->redirect('postcard_sent');
	}
}
?>